//用来造home页面的数据
//引入mockjs
import Mock from 'mockjs'
//引入url工具
import {
    param2Obj
} from '@/utils/urlUtils'
//数据库
//1环境温度
var outdoorTemperature = []
Temper(outdoorTemperature, 15, 39)
//2环境湿度
var environmentalHumidity = []
Temper(environmentalHumidity, 20, 70)
//3土壤湿度
var soilMoisture = []
Temper(soilMoisture, 10, 70)
//4大棚数据
// 室内湿度40-60
// 土壤湿度17-44
var criticalList = []
var criticalObj = {
    id: 1,
    name: "西瓜棚",
    temp: 36,
    humidity: 72,
    tdght: 70,
    moisture: 60,
    state: "1", //1正常，2处理中，3异常
    note: "",
}
for (var i = 0; i < 15; i++) {
    criticalObj.id = i + 1
    criticalObj.state = Mock.mock('@natural(1, 3)')
    criticalList.push(JSON.parse(JSON.stringify(criticalObj)))
}
//5代办事项
var toDoNum1 = 10
var toDoNum2 = 3
var toDoNum3 = 2
var toDoNum1_1 = Math.round(toDoNum1 / (toDoNum1 + toDoNum2 + toDoNum3) * 100)
var toDoNum2_1 = Math.round(toDoNum2 / (toDoNum1 + toDoNum2 + toDoNum3) * 100)
var toDoNum3_1 = 100 - toDoNum1_1 - toDoNum2_1
var toDoList = [
    [toDoNum1, toDoNum1_1],
    [toDoNum2, toDoNum2_1],
    [toDoNum3, toDoNum3_1]
]
//6sBut里的六个数据
var sButList = [35, 36, 50, 62, 100, 61]
//7地图数据
var mapList = [
    {
        id: 1,
        name: '西瓜棚',
        geoCoordMap: [114.31, 30.52],
        value: 500
    },
    {
        id: 2,
        name: '草莓棚',
        geoCoordMap: [119.64, 29.12],
        value: 300
    },
    {
        id: 3,
        name: '甜瓜棚',
        geoCoordMap: [115.97, 29.71],
        value: 200
    },
    {
        id: 4,
        name: '番茄棚',
        geoCoordMap: [117, 36.65],
        value: 100
    },
    {
        id: 5,
        name: '甜瓜棚',
        geoCoordMap: [112, 27],
        value: 400
    },
    {
        id: 6,
        name: '番茄棚',
        geoCoordMap: [110, 36],
        value: 350
    }
]






//数据抛出
export default {
    sBut: config => {
        // console.log('config',param2Obj(config.url));
        // var { keyword } = param2Obj(config.url)
        // var res =  List.filter(item=>item.username.indexOf(keyword) != -1)
        var res = sButList
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    toD: config => {
        // console.log('config',param2Obj(config.url));
        // var { keyword } = param2Obj(config.url)
        // var res =  List.filter(item=>item.username.indexOf(keyword) != -1)
        var res = toDoList
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    outdoorTemp: config => {
        // console.log('config', config)
        if (config.body) {
            let keyword = JSON.parse(config.body)
            // console.log('keyword', keyword)
            // console.log('outdoorTemperature', outdoorTemperature)
            var res = outdoorTemperature.filter(item => keyword.includes(item.date))
        } else {
            var res = outdoorTemperature
        }
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    environmentalHum: config => {
        // console.log('config', config)
        if (config.body) {
            let keyword = JSON.parse(config.body)
            // console.log('keyword', keyword)
            // console.log('environmentalHumidity', environmentalHumidity)
            var res = environmentalHumidity.filter(item => keyword.includes(item.date))
        } else {
            var res = environmentalHumidity
        }
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    soilMois: config => {
        // console.log('config', config)
        if (config.body) {
            let keyword = JSON.parse(config.body)
            // console.log('keyword', keyword)
            // console.log('soilMoisture', soilMoisture)
            var res = soilMoisture.filter(item => keyword.includes(item.date))
        } else {
            var res = soilMoisture
        }
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    critical: config => {
        // console.log('config1', param2Obj(config.url));
        var { keyword } = param2Obj(config.url)
        if (keyword == "0") {
            var res = criticalList
        } else {
            // var res =  criticalList.filter(item=>item.state.indexOf(keyword) != -1)
            var res = criticalList.filter(item => item.state == keyword)
        }

        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
    MapData: config => {
        var res = mapList
        return {
            code: 200,
            msg: '请求成功',
            data: res
        }
    },
}


//历史数据函数


function Temper(indoorTemperature, num1, num2) {
    var time = Mock.mock("@now('hour')").split(' ')
    var time0 = time[0]
    var time1 = time[1]
    var time0 = time0.split('-')
    var time1 = time[1]
    var time1 = time1.split(':')
    for (var i = 0; i < 100; i++) {
        let a = new Date(time0[0], time0[1] - 1, time0[2] - i)
        let year = a.getFullYear()
        let month = a.getMonth()
        let day = a.getDate()
        let average = 0
        let b = []
        if (i == 0) {
            for (let j = 0; j <= time1[0]; j++) {
                b.push(Mock.mock(`@integer(${num1}, ${num2})`))
            }
            average = b.reduce((prev, item) => prev + item, 0) / time1[0]
        } else {
            for (let j = 0; j < 24; j++) {
                b.push(Mock.mock(`@integer(${num1}, ${num2})`))
            }
            average = b.reduce((prev, item) => prev + item, 0) / 24
        }
        // indoorTemperature[`${year}${month}${day}`] = b
        average = Math.round(average)
        indoorTemperature.push({
            date: `${year}/${month + 1}/${day}`,
            temp: b,
            average
        })
    }
}